package cc.vicp.djx314.starfood.service.dishes.impls;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cc.vicp.djx314.starfood.dao.dish.DishMapper;
import cc.vicp.djx314.starfood.domain.dish.Dish;
import cc.vicp.djx314.starfood.domain.dish.ProvideTime;
import cc.vicp.djx314.starfood.service.dishes.DishService;

@Service
public class DishServiceImpl implements DishService {
	@Autowired
	DishMapper dishMapper;

	@Override
	@Transactional(readOnly = true)
	public List<Dish> getDishAll() {
		List<Dish> dishes = dishMapper.getCacheDishAll();
		if (dishes == null) {
			dishes = new ArrayList<Dish>();
		}
		return dishes;
	}

	@Override
	@Transactional(readOnly = false)
	public void createDish(Dish dish, byte[] dishImgFile) {
		dishMapper.createCacheDish(dish, dishImgFile);
		Integer dishId = dish.getId();
		for (ProvideTime pTime : dish.getProvideTimes()) {
			if (pTime != null) {
				dishMapper.createCacheProvideTime(pTime);
				dishMapper.createCacheTimeDish(dishId, pTime.getId());
			}
		}
	}
}